"""
从智图转到a层
"""
import facade
from xjlibrary.mdatetime.mtime2 import MDateTimeUtils


class ZTToATable(object):

    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.new_db3_path = "./libvideo.db3"
        self.zt = facade.Sqlite3Utiles(logger=self.logger).Sqlite3DBConnectFromFilePath(
            self.new_db3_path, encoding="utf-8")
        self.a = facade.Sqlite3Utiles(logger=self.logger).Sqlite3DBConnectFromFilePath(
            "./a_libvideo.db3", encoding="utf-8")

    # 转移字符
    def sqliteEscape(self, keyWord):
        keyWord = keyWord.replace('/', '//')
        keyWord = keyWord.replace("'", "''")
        keyWord = keyWord.replace("[", "/[")
        keyWord = keyWord.replace("]", "/]")
        keyWord = keyWord.replace("%", "/%")
        keyWord = keyWord.replace("&", "/&")
        keyWord = keyWord.replace("_", "/_")
        keyWord = keyWord.replace("(", "/(")
        keyWord = keyWord.replace(")", "/)")
        return keyWord

    def select(self):
        sql = "select lngid,rawid,title,`description`,provider_subject,`date`,date_created,`language`,country,provider_url,cover,folio_size,type from modify_title_info_zt"
        rows = self.zt.SelectFromSqlliteFetchall(sql)
        sub_db_id = "00153"
        product = "LIBVIDEO"
        sub_db = "DMT"
        provider_zt = "libvideo"
        provider = "YUANLAI"
        down_date = MDateTimeUtils.get_today_date_strings()
        batch = MDateTimeUtils.datetime_to_string(MDateTimeUtils.get_now_datetime(), '%Y%m%d_%H%M%S')
        sqlList = []
        for row in rows:
            lngid, rawid, title, description, provider_subject, date, date_created, language, country, provider_url, cover, folio_size, type = row
            abstract = description
            abstract = self.sqliteEscape(abstract)
            title = self.sqliteEscape(title)
            subject = provider_subject
            subject = self.sqliteEscape(subject)
            pub_year = date
            pub_date = date_created
            provider_url = provider_url.replace("libvideo@", "")
            book_size = folio_size
            source_type = type
            cover_path = cover
            publisher = "武汉缘来文化传播有限责任公司"

            sql = "insert into base_obj_meta_a (lngid,rawid,title,abstract,`subject`,pub_year,pub_date,`language`,country,provider_url,cover_path,book_size,source_type,sub_db_id,product,sub_db,provider,down_date,batch,publisher) values ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(
                lngid, rawid, title, abstract, subject, pub_year, pub_date, language, country, provider_url, cover_path,
                book_size, source_type, sub_db_id, product, sub_db, provider, down_date, batch, publisher)

            sqlList.append(sql)
        self.a.ExeSqlliteList(sqlList)


def main():
    zta = ZTToATable()
    zta.select()


if __name__ == "__main__":
    main()
